home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / emacs.lha / emacs-19.16 / lisp / isearch.el < prev    next >
Lisp/Scheme  |  1993-07-02  |  51KB  |  1,408 lines

  1. ;;; isearch.el --- incremental search minor mode.
  2.  
  3. ;; Copyright (C) 1992, 1993 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Daniel LaLiberte <liberte@cs.uiuc.edu>
  6.  
  7. ;; |$Date: 1993/07/02 21:34:15 $|$Revision: 1.44 $
  8.  
  9. ;; This file is not yet part of GNU Emacs, but it is based almost
  10. ;; entirely on isearch.el which is part of GNU Emacs.
  11.  
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY.  No author or distributor
  14. ;; accepts responsibility to anyone for the consequences of using it
  15. ;; or for whether it serves any particular purpose or works at all,
  16. ;; unless he says so in writing.  Refer to the GNU Emacs General Public
  17. ;; License for full details.
  18.  
  19. ;; Everyone is granted permission to copy, modify and redistribute
  20. ;; GNU Emacs, but only under the conditions described in the
  21. ;; GNU Emacs General Public License.   A copy of this license is
  22. ;; supposed to have been given to you along with GNU Emacs so you
  23. ;; can know your rights and responsibilities.  It should be in a
  24. ;; file named COPYING.  Among other things, the copyright notice
  25. ;; and this notice must be preserved on all copies.
  26.  
  27. ;;; Commentary:
  28.  
  29. ;;;====================================================================
  30. ;; Instructions
  31.  
  32. ;; Searching with isearch-mode.el should work just like isearch.el,
  33. ;; except it is done in a temporary minor mode that terminates when
  34. ;; you finish searching.
  35.  
  36. ;; To use isearch-mode instead of the standard isearch.el, add the
  37. ;; following to your .emacs file.  The standard key bindings to
  38. ;; isearch-forward, etc, will then use isearch-mode instead of
  39. ;; isearch.
  40.  
  41. ;; (defalias 'isearch 'isearch-mode)
  42. ;; (autoload 'isearch-mode "isearch-mode")
  43.  
  44. ;; For programmed use of isearch-mode, e.g. calling (isearch-forward),
  45. ;; isearch-mode behaves modally and does not return until the search
  46. ;; is completed.  It uses a recursive-edit to behave this way.  Note:
  47. ;; gnus does it wrong: (call-interactively 'isearch-forward).
  48.  
  49. ;; If any package you use invokes isearching non-interactively to get
  50. ;; the modal behavior described above, you must use the redefinitions
  51. ;; of isearch-forward, etc. found in this file instead of those in
  52. ;; loaddefs.el.  The simplest way to ensure this is to just load
  53. ;; isearch-mode explicitly in your .emacs instead of using the above
  54. ;; defalias and autoload.
  55.  
  56. ;; (load "isearch-mode")
  57.  
  58. ;; The key bindings active within isearch-mode are defined below in
  59. ;; `isearch-mode-map' which is given bindings close to the default
  60. ;; characters of the original isearch.el.  With `isearch-mode',
  61. ;; however, you can bind multi-character keys and it should be easier
  62. ;; to add new commands.  One bug though: keys with meta-prefix cannot
  63. ;; be longer than two chars.  Also see minibuffer-local-isearch-map
  64. ;; for bindings active during `isearch-edit-string'.
  65.  
  66. ;; Note to emacs version 19 users: isearch-mode should work even if
  67. ;; you switch windows with the mouse, in which case isearch-mode is
  68. ;; terminated automatically before the switch.  This is true of lemacs
  69. ;; too, with a few more cleanups I've neglected in this release. 
  70. ;; No one has supplied patches for epoch yet.
  71.  
  72. ;; The search ring and completion commands automatically put you in
  73. ;; the minibuffer to edit the string.  This gives you a chance to
  74. ;; modify the search string before executing the search.  There are
  75. ;; three commands to terminate the editing: C-s and C-r exit the
  76. ;; minibuffer and search forward and reverse respectively, while C-m
  77. ;; exits and does a nonincremental search.
  78.  
  79. ;; Exiting immediately from isearch uses isearch-edit-string instead
  80. ;; of nonincremental-search, if search-nonincremental-instead is non-nil.
  81. ;; The name of this option should probably be changed if we decide to
  82. ;; keep the behavior.  No point in forcing nonincremental search until
  83. ;; the last possible moment.
  84.  
  85. ;; TODO
  86. ;; - Integrate the emacs 19 generalized command history.
  87. ;; - Think about incorporating query-replace.
  88. ;; - Hooks and options for failed search.
  89.  
  90. ;;; Change Log:
  91.  
  92. ;;; Changes before those recorded in ChangeLog:
  93.  
  94. ;;; Revision 1.4  92/09/14  16:26:02  liberte
  95. ;;; Added prefix args to isearch-forward, etc. to switch between
  96. ;;;    string and regular expression searching.
  97. ;;; Added some support for lemacs.
  98. ;;; Added general isearch-highlight option - but only for lemacs so far.
  99. ;;; Added support for frame switching in emacs 19.
  100. ;;; Added word search option to isearch-edit-string.
  101. ;;; Renamed isearch-quit to isearch-abort.
  102. ;;; Numerous changes to comments and doc strings.
  103. ;;; 
  104. ;;; Revision 1.3  92/06/29  13:10:08  liberte
  105. ;;; Moved modal isearch-mode handling into isearch-mode.
  106. ;;; Got rid of buffer-local isearch variables.
  107. ;;; isearch-edit-string used by ring adjustments, completion, and
  108. ;;; nonincremental searching.  C-s and C-r are additional exit commands.
  109. ;;; Renamed all regex to regexp.
  110. ;;; Got rid of found-start and found-point globals.
  111. ;;; Generalized handling of upper-case chars.
  112.  
  113. ;;; Revision 1.2  92/05/27  11:33:57  liberte
  114. ;;; Emacs version 19 has a search ring, which is supported here.
  115. ;;; Other fixes found in the version 19 isearch are included here.
  116. ;;;
  117. ;;; Also see variables search-caps-disable-folding,
  118. ;;; search-nonincremental-instead, search-whitespace-regexp, and
  119. ;;; commands isearch-toggle-regexp, isearch-edit-string.
  120. ;;;
  121. ;;; semi-modal isearching is supported.
  122.  
  123. ;;; Changes for 1.1
  124. ;;; 3/18/92 Fixed invalid-regexp.
  125. ;;; 3/18/92 Fixed yanking in regexps.
  126.  
  127. ;;; Code:
  128.  
  129.  
  130. ;;;=========================================================================
  131. ;;; Emacs features
  132.  
  133. ;; isearch-mode takes advantage of the features provided by several
  134. ;; different versions of emacs.  Rather than testing the version of
  135. ;; emacs, several constants are defined, one for each of the features.
  136. ;; Each of the tests below must work on any version of emacs.
  137. ;; (Perhaps provide and featurep could be used for this purpose.)
  138.  
  139. (defconst isearch-gnu-emacs-events (fboundp 'set-frame-height)) ;; emacs 19
  140. (defconst isearch-pre-command-hook-exists (boundp 'pre-command-hook)) ;; lemacs
  141. (defconst isearch-event-data-type nil)  ;; lemacs
  142.  
  143. (defconst search-exit-option t
  144.   "*Non-nil means random control characters terminate incremental search.")
  145.  
  146. (defvar search-slow-window-lines 1
  147.   "*Number of lines in slow search display windows.
  148. These are the short windows used during incremental search on slow terminals.
  149. Negative means put the slow search window at the top (normally it's at bottom)
  150. and the value is minus the number of lines.")
  151.  
  152. (defvar search-slow-speed 1200
  153.   "*Highest terminal speed at which to use \"slow\" style incremental search.
  154. This is the style where a one-line window is created to show the line
  155. that the search has reached.")
  156.  
  157. ;;;========================================================================
  158. ;;; Some additional options and constants.
  159.  
  160. (defvar search-upper-case 'not-yanks
  161.   "*If non-nil, upper case chars disable case fold searching.
  162. That is, upper and lower case chars must match exactly.
  163. This applies no matter where the chars come from, but does not
  164. apply to chars in regexps that are prefixed with `\\'.
  165. If this value is `not-yanks', yanked text is always downcased.")
  166.  
  167. (defvar search-nonincremental-instead t
  168.   "*If non-nil, do a nonincremental search instead if exiting immediately.
  169. Actually, `isearch-edit-string' is called to let you enter the search
  170. string, and RET terminates editing and does a nonincremental search.")
  171.  
  172. (defconst search-whitespace-regexp "\\s-+"
  173.   "*If non-nil, regular expression to match a sequence of whitespace chars.
  174. You might want to use something like \"[ \\t\\r\\n]+\" instead.")
  175.  
  176. ;; I removed the * from the doc string because highlighting is not 
  177. ;; currently a clean thing to do.  Once highlighting is made clean, 
  178. ;; this feature can be re-enabled and advertised.
  179. (defvar search-highlight nil
  180.   "*Non-nil means incremental search highlights the current match.")
  181.  
  182. (defvar isearch-mode-hook nil
  183.   "Function(s) to call after starting up an incremental search.")
  184.  
  185. (defvar isearch-mode-end-hook nil
  186.   "Function(s) to call after terminating an incremental search.")
  187.  
  188. ;;;==================================================================
  189. ;;; Search ring.
  190.  
  191. (defvar search-ring nil
  192.   "List of search string sequences.")
  193. (defvar regexp-search-ring nil
  194.   "List of regular expression search string sequences.")
  195.  
  196. (defconst search-ring-max 16
  197.   "*Maximum length of search ring before oldest elements are thrown away.")
  198. (defconst regexp-search-ring-max 16
  199.   "*Maximum length of regexp search ring before oldest elements are thrown away.")
  200.  
  201. (defvar search-ring-yank-pointer nil
  202.   "Index in `search-ring' of last string reused.
  203. nil if none yet.")
  204. (defvar regexp-search-ring-yank-pointer nil
  205.   "Index in `regexp-search-ring' of last string reused.
  206. nil if none yet.")
  207.  
  208. (defvar search-ring-update nil
  209.   "*Non-nil if advancing or retreating in the search ring should cause search.
  210. Default value, nil, means edit the string instead.")
  211.  
  212. ;;;====================================================
  213. ;;; Define isearch-mode keymap.
  214.  
  215. (defvar isearch-mode-map nil
  216.   "Keymap for isearch-mode.")
  217.  
  218. (or isearch-mode-map
  219.     (let* ((i 0)
  220.        (map (make-keymap)))
  221.       (or (vectorp (nth 1 map))
  222.       (error "The initialization of isearch-mode-map must be updated"))
  223.       ;; Give this map a vector 256 long, for dense binding
  224.       ;; of a larger range of ordinary characters.
  225.       (setcar (cdr map) (make-vector 256 nil))
  226.  
  227.       ;; Make function keys, etc, exit the search.
  228.       (define-key map [t] 'isearch-other-control-char)
  229.       ;; Control chars, by default, end isearch mode transparently.
  230.       ;; We need these explicit definitions because, in a dense keymap, 
  231.       ;; the binding for t does not affect characters.
  232.       ;; We use a dense keymap to save space.
  233.       (while (< i ?\ )
  234.     (define-key map (make-string 1 i) 'isearch-other-control-char)
  235.     (setq i (1+ i)))
  236.  
  237.       ;; Printing chars extend the search string by default.
  238.       (setq i ?\ )
  239.       (while (< i (length (nth 1 map)))
  240.     (define-key map (vector i) 'isearch-printing-char)
  241.     (setq i (1+ i)))
  242.  
  243.       ;; Several non-printing chars change the searching behavior.
  244.       (define-key map "\C-s" 'isearch-repeat-forward)
  245.       (define-key map "\C-r" 'isearch-repeat-backward)
  246.       (define-key map "\177" 'isearch-delete-char)
  247.       (define-key map "\C-g" 'isearch-abort)
  248.     
  249.       (define-key map "\C-q" 'isearch-quote-char)
  250.  
  251.       (define-key map "\r" 'isearch-exit)
  252.       (define-key map "\C-j" 'isearch-printing-char)
  253.       (define-key map "\t" 'isearch-printing-char)
  254.       (define-key map " " 'isearch-whitespace-chars)
  255.     
  256.       (define-key map "\C-w" 'isearch-yank-word)
  257.       (define-key map "\C-y" 'isearch-yank-line)
  258.  
  259.       ;; Bind the ASCII-equivalent "function keys" explicitly
  260.       ;; if we bind their equivalents, 
  261.       ;; since otherwise the default binding would override.
  262.       ;; We bind [escape] below.
  263.       (define-key map [tab] 'isearch-printing-char)
  264.       (define-key map [delete] 'isearch-delete-char)
  265.       (define-key map [backspace] 'isearch-delete-char)
  266.       (define-key map [return] 'isearch-exit)
  267.       (define-key map [newline] 'isearch-printing-char)
  268.  
  269.       ;; Define keys for regexp chars * ? |.
  270.       ;; Nothing special for + because it matches at least once.
  271.       (define-key map "*" 'isearch-*-char)
  272.       (define-key map "?" 'isearch-*-char)
  273.       (define-key map "|" 'isearch-|-char)
  274.  
  275. ;;; Turned off because I find I expect to get the global definition--rms.
  276. ;;;      ;; Instead bind C-h to special help command for isearch-mode.
  277. ;;;      (define-key map "\C-h" 'isearch-mode-help)
  278.  
  279.       ;; To handle local bindings with meta char prefix keys, define
  280.       ;; another full keymap.  This must be done for any other prefix
  281.       ;; keys as well, one full keymap per char of the prefix key.  It
  282.       ;; would be simpler to disable the global keymap, and/or have a
  283.       ;; default local key binding for any key not otherwise bound.
  284.       (let ((meta-map (make-sparse-keymap)))
  285.     (define-key map (char-to-string meta-prefix-char) meta-map)
  286.     (define-key map [escape] meta-map))
  287.       (define-key map (vector meta-prefix-char t) 'isearch-other-meta-char)
  288.  
  289.       (define-key map "\M-n" 'isearch-ring-advance)
  290.       (define-key map "\M-p" 'isearch-ring-retreat)
  291.  
  292.       (define-key map "\M-\t" 'isearch-complete)
  293.  
  294.       ;; For emacs 19, switching frames should terminate isearch-mode
  295.       (if isearch-gnu-emacs-events
  296.       (define-key map [switch-frame] 'isearch-switch-frame-handler))
  297.       
  298.       (setq isearch-mode-map map)
  299.       ))
  300.  
  301. ;; Some bindings you may want to put in your isearch-mode-hook.
  302. ;; Suggest some alternates...
  303. ;; (define-key isearch-mode-map "\C-t" 'isearch-toggle-regexp)
  304. ;; (define-key isearch-mode-map "\C-^" 'isearch-edit-string)
  305.  
  306.  
  307. (defvar minibuffer-local-isearch-map nil
  308.   "Keymap for editing isearch strings in the minibuffer.")
  309.  
  310. (or minibuffer-local-isearch-map
  311.     (let ((map (copy-keymap minibuffer-local-map)))
  312.       (define-key map "\r" 'isearch-nonincremental-exit-minibuffer)
  313.       (define-key map "\M-n" 'isearch-ring-advance-edit)
  314.       (define-key map "\M-p" 'isearch-ring-retreat-edit)
  315.       (define-key map "\M-\t" 'isearch-complete-edit)
  316.       (define-key map "\C-s" 'isearch-forward-exit-minibuffer)
  317.       (define-key map "\C-r" 'isearch-reverse-exit-minibuffer)
  318.       (setq minibuffer-local-isearch-map map)
  319.       ))
  320.  
  321. ;;;========================================================
  322. ;; Internal variables declared globally for byte-compiler.
  323. ;; These are all set with setq while isearching
  324. ;; and bound locally while editing the search string.
  325.  
  326. (defvar isearch-forward nil)    ; Searching in the forward direction.
  327. (defvar isearch-regexp nil)    ; Searching for a regexp.
  328. (defvar isearch-word nil)    ; Searching for words.
  329.  
  330. (defvar isearch-cmds nil)   ; Stack of search status sets.
  331. (defvar isearch-string "")  ; The current search string.
  332. (defvar isearch-message "") ; text-char-description version of isearch-string
  333.  
  334. (defvar isearch-success t)        ; Searching is currently successful.
  335. (defvar isearch-invalid-regexp nil)    ; Regexp not well formed.
  336. (defvar isearch-other-end nil)    ; Start (end) of match if forward (backward).
  337. (defvar isearch-wrapped nil)    ; Searching restarted from the top (bottom).
  338. (defvar isearch-barrier 0)
  339.  
  340. (defvar isearch-case-fold-search nil) ; case-fold-search while searching.
  341.  
  342. (defvar isearch-adjusted nil)
  343. (defvar isearch-slow-terminal-mode nil)
  344. ;;; If t, using a small window.
  345. (defvar isearch-small-window nil)
  346. (defvar isearch-opoint 0)
  347. ;;; The window configuration active at the beginning of the search.
  348. (defvar isearch-window-configuration nil)
  349. (defvar isearch-old-local-map nil)
  350.  
  351. ;; Flag to indicate a yank occurred, so don't move the cursor.
  352. (defvar isearch-yank-flag nil)
  353.  
  354. ;;; A function to be called after each input character is processed.
  355. ;;; (It is not called after characters that exit the search.)
  356. ;;; It is only set from an optional argument to `isearch-mode'.
  357. (defvar isearch-op-fun nil)
  358.  
  359. ;;;  Is isearch-mode in a recursive edit for modal searching.
  360. (defvar isearch-recursive-edit nil)
  361.  
  362. ;;; Should isearch be terminated after doing one search?
  363. (defvar isearch-nonincremental nil)
  364.  
  365. ;; New value of isearch-forward after isearch-edit-string.
  366. (defvar isearch-new-forward nil)
  367.  
  368.  
  369. ;;;==============================================================
  370. ;; Minor-mode-alist changes - kind of redundant with the
  371. ;; echo area, but if isearching in multiple windows, it can be useful.
  372.  
  373. (or (assq 'isearch-mode minor-mode-alist)
  374.     (nconc minor-mode-alist
  375.        (list '(isearch-mode isearch-mode))))
  376.  
  377. (defvar isearch-mode nil) ;; Name of the minor mode, if non-nil.
  378. (make-variable-buffer-local 'isearch-mode)
  379.  
  380. (define-key global-map "\C-s" 'isearch-forward)
  381. (define-key esc-map "\C-s" 'isearch-forward-regexp)
  382. (define-key global-map "\C-r" 'isearch-backward)
  383. (define-key esc-map "\C-r" 'isearch-backward-regexp)
  384.  
  385. ;;;===============================================================
  386. ;;; Entry points to isearch-mode.
  387. ;;; These four functions should replace those in loaddefs.el
  388. ;;; An alternative is to defalias isearch-forward etc to isearch-mode,
  389. ;;; and look at this-command to set the options accordingly.
  390.  
  391. (defun isearch-forward (&optional regexp-p no-recursive-edit)
  392.   "\
  393. Do incremental search forward.
  394. With a prefix argument, do an incremental regular expression search instead.
  395. \\<isearch-mode-map>
  396. As you type characters, they add to the search string and are found.
  397. The following non-printing keys are bound in `isearch-mode-map'.  
  398.  
  399. Type \\[isearch-delete-char] to cancel characters from end of search string.
  400. Type \\[isearch-exit] to exit, leaving point at location found.
  401. Type LFD (C-j) to match end of line.
  402. Type \\[isearch-repeat-forward] to search again forward,\
  403.  \\[isearch-repeat-backward] to search again backward.
  404. Type \\[isearch-yank-word] to yank word from buffer onto end of search\
  405.  string and search for it.
  406. Type \\[isearch-yank-line] to yank rest of line onto end of search string\
  407.  and search for it.
  408. Type \\[isearch-quote-char] to quote control character to search for it.
  409. \\[isearch-abort] while searching or when search has failed cancels input\
  410.  back to what has
  411.  been found successfully.
  412. \\[isearch-abort] when search is successful aborts and moves point to\
  413.  starting point.
  414.  
  415. Also supported is a search ring of the previous 16 search strings.
  416. Type \\[isearch-ring-advance] to search for the next item in the search ring.
  417. Type \\[isearch-ring-retreat] to search for the previous item in the search\
  418.  ring.
  419. Type \\[isearch-complete] to complete the search string using the search ring.
  420.  
  421. The above keys, bound in `isearch-mode-map', are often controlled by 
  422.  options; do M-x apropos on search-.* to find them.
  423. Other control and meta characters terminate the search
  424.  and are then executed normally (depending on `search-exit-option').
  425.  
  426. If this function is called non-interactively, it does not return to
  427. the calling function until the search is done."
  428.  
  429.   (interactive "P\np")
  430.   (isearch-mode t (not (null regexp-p)) nil (not no-recursive-edit)))
  431.  
  432. (defun isearch-forward-regexp (&optional not-regexp no-recursive-edit)
  433.   "\
  434. Do incremental search forward for regular expression.
  435. With a prefix argument, do a regular string search instead.
  436. Like ordinary incremental search except that your input
  437. is treated as a regexp.  See \\[isearch-forward] for more info."
  438.   (interactive "P\np")
  439.   (isearch-mode t (null not-regexp) nil (not no-recursive-edit)))
  440.  
  441. (defun isearch-backward (&optional regexp-p no-recursive-edit)
  442.   "\
  443. Do incremental search backward.
  444. With a prefix argument, do a regular expression search instead.
  445. See \\[isearch-forward] for more information."
  446.   (interactive "P\np")
  447.   (isearch-mode nil (not (null regexp-p)) nil (not no-recursive-edit)))
  448.  
  449. (defun isearch-backward-regexp (&optional not-regexp no-recursive-edit)
  450.   "\
  451. Do incremental search backward for regular expression.
  452. With a prefix argument, do a regular string search instead.
  453. Like ordinary incremental search except that your input
  454. is treated as a regexp.  See \\[isearch-forward] for more info."
  455.   (interactive "P\np")
  456.   (isearch-mode nil (null not-regexp) nil (not no-recursive-edit)))
  457.  
  458.  
  459. (defun isearch-mode-help ()
  460.   (interactive)
  461.   (describe-function 'isearch-forward)
  462.   (isearch-update))
  463.  
  464.  
  465. ;;;==================================================================
  466. ;; isearch-mode only sets up incremental search for the minor mode.
  467. ;; All the work is done by the isearch-mode commands.
  468.  
  469. ;; Not used yet:
  470. ;;(defconst isearch-commands '(isearch-forward isearch-backward
  471. ;;                 isearch-forward-regexp isearch-backward-regexp)
  472. ;;  "List of commands for which isearch-mode does not recursive-edit.")
  473.                  
  474.  
  475. (defun isearch-mode (forward &optional regexp op-fun recursive-edit word-p)
  476.   "Start isearch minor mode.  Called by isearch-forward, etc."
  477.  
  478.   ;; Initialize global vars.
  479.   (setq isearch-forward forward
  480.     isearch-regexp regexp
  481.     isearch-word word-p
  482.     isearch-op-fun op-fun
  483.     isearch-case-fold-search case-fold-search
  484.     isearch-string ""
  485.     isearch-message ""
  486.     isearch-cmds nil
  487.     isearch-success t
  488.     isearch-wrapped nil
  489.     isearch-barrier (point)
  490.     isearch-adjusted nil
  491.     isearch-yank-flag nil
  492.     isearch-invalid-regexp nil
  493.     ;; Use (baud-rate) for now, for sake of other versions.
  494.     isearch-slow-terminal-mode (and (<= (baud-rate) search-slow-speed)
  495.                     (> (window-height)
  496.                        (* 4 search-slow-window-lines)))
  497.     isearch-other-end nil
  498.     isearch-small-window nil
  499.  
  500.     isearch-opoint (point)
  501.     isearch-old-local-map (current-local-map)
  502.     search-ring-yank-pointer nil
  503.     regexp-search-ring-yank-pointer nil)
  504.   (if isearch-slow-terminal-mode
  505.       (setq isearch-window-configuration (current-window-configuration)))
  506.  
  507. ;; This was for Lucid Emacs.  But now that we have pre-command-hook,
  508. ;; it causes trouble.
  509. ;;  (if isearch-pre-command-hook-exists
  510. ;;      (add-hook 'pre-command-hook 'isearch-pre-command-hook))
  511.   (setq    isearch-mode " Isearch")  ;; forward? regexp?
  512.   (set-buffer-modified-p (buffer-modified-p)) ; update modeline
  513.  
  514.   ;; It is ugly to show region highlighting while the search
  515.   ;; is going on.  And we don't want the mark active at the end either.
  516.   (setq deactivate-mark t)      
  517.  
  518.   (isearch-push-state)
  519.  
  520.   (use-local-map isearch-mode-map)
  521.   (isearch-update)
  522.   (run-hooks 'isearch-mode-hook)
  523.  
  524.   ;; isearch-mode can be made modal (in the sense of not returning to 
  525.   ;; the calling function until searching is completed) by entering 
  526.   ;; a recursive-edit and exiting it when done isearching.
  527.   (if recursive-edit
  528.       (let ((isearch-recursive-edit t))
  529.     (recursive-edit)))
  530.   )
  531.  
  532.  
  533. ;;;====================================================
  534. ;; Some high level utilities.  Others below.
  535.  
  536. (defun isearch-update ()
  537.   ;; Called after each command to update the display.  
  538.   (if (if isearch-event-data-type
  539.       (null unread-command-event)
  540.     (if isearch-gnu-emacs-events
  541.         (null unread-command-events)
  542.       (< unread-command-char 0)))
  543.       (progn
  544.     (if (not (input-pending-p))
  545.         (isearch-message))
  546.     (if (and isearch-slow-terminal-mode
  547.          (not (or isearch-small-window 
  548.               (pos-visible-in-window-p))))
  549.         (let ((found-point (point)))
  550.           (setq isearch-small-window t)
  551.           (move-to-window-line 0)
  552.           (let ((window-min-height 1))
  553.         (split-window nil (if (< search-slow-window-lines 0)
  554.                       (1+ (- search-slow-window-lines))
  555.                     (- (window-height)
  556.                        (1+ search-slow-window-lines)))))
  557.           (if (< search-slow-window-lines 0)
  558.           (progn (vertical-motion (- 1 search-slow-window-lines))
  559.              (set-window-start (next-window) (point))
  560.              (set-window-hscroll (next-window)
  561.                          (window-hscroll))
  562.              (set-window-hscroll (selected-window) 0))
  563.         (other-window 1))
  564.           (goto-char found-point)))
  565.     (if isearch-other-end
  566.         (if (< isearch-other-end (point)) ; isearch-forward?
  567.         (isearch-highlight isearch-other-end (point))
  568.           (isearch-highlight (point) isearch-other-end)))
  569.     ))
  570.   (setq ;; quit-flag nil  not for isearch-mode
  571.    isearch-adjusted nil
  572.    isearch-yank-flag nil)
  573.   )
  574.  
  575.  
  576. (defun isearch-done (&optional nopush)
  577.   ;; Called by all commands that terminate isearch-mode.
  578.   ;; If NOPUSH is non-nil, we don't push the string on the search ring.
  579.   (use-local-map isearch-old-local-map)
  580.   ;; (setq pre-command-hook isearch-old-pre-command-hook) ; for lemacs
  581.   (isearch-dehighlight t)
  582.   (let ((found-start (window-start (selected-window)))
  583.     (found-point (point)))
  584.     (if isearch-window-configuration
  585.     (set-window-configuration isearch-window-configuration))
  586.  
  587.     ;; If there was movement, mark the starting position.
  588.     ;; Maybe should test difference between and set mark iff > threshold.
  589.     (if (/= (point) isearch-opoint)
  590.     (progn
  591.       (push-mark isearch-opoint t)
  592.       (if transient-mark-mode
  593.           (setq mark-active nil))
  594.       (or executing-macro (> (minibuffer-depth) 0)
  595.           (message "Mark saved where search started")))
  596.       ;; (message "") why is this needed?
  597.       )
  598.     (if isearch-small-window
  599.     (goto-char found-point)
  600.       ;; Exiting the save-window-excursion clobbers window-start; restore it.
  601.       (set-window-start (selected-window) found-start t)))
  602.  
  603.   (setq isearch-mode nil)
  604.   (set-buffer-modified-p (buffer-modified-p))  ;; update modeline
  605.  
  606.   (if (and (> (length isearch-string) 0) (not nopush))
  607.       ;; Update the ring data.
  608.       (if isearch-regexp 
  609.       (if (or (null regexp-search-ring)
  610.           (not (string= isearch-string (car regexp-search-ring))))
  611.           (progn
  612.         (setq regexp-search-ring
  613.               (cons isearch-string regexp-search-ring))
  614.         (if (> (length regexp-search-ring) regexp-search-ring-max)
  615.             (setcdr (nthcdr (1- search-ring-max) regexp-search-ring)
  616.                 nil))))
  617.     (if (or (null search-ring)
  618.         (not (string= isearch-string (car search-ring))))
  619.         (progn
  620.           (setq search-ring (cons isearch-string search-ring))
  621.           (if (> (length search-ring) search-ring-max)
  622.           (setcdr (nthcdr (1- search-ring-max) search-ring) nil))))))
  623.  
  624.   (run-hooks 'isearch-mode-end-hook)
  625.   (if isearch-recursive-edit (exit-recursive-edit)))
  626.  
  627. ;;;=======================================================
  628. ;;; Switching buffers should first terminate isearch-mode.
  629. ;;; This is done quite differently for each variant of emacs.
  630. ;;; For lemacs, see Exiting in lemacs below
  631.  
  632. ;; For Emacs 19, the frame switch event is handled.
  633. (defun isearch-switch-frame-handler ()
  634.   (interactive) ;; Is this necessary?
  635.   ;; First terminate isearch-mode.
  636.   (isearch-done)
  637.   (handle-switch-frame (car (cdr (isearch-last-command-char)))))
  638.  
  639. ;;;========================================================
  640.  
  641.  
  642. ;;;====================================================
  643. ;; Commands active while inside of the isearch minor mode.
  644.  
  645. (defun isearch-exit ()
  646.   "Exit search normally.
  647. However, if this is the first command after starting incremental
  648. search and `search-nonincremental-instead' is non-nil, do a
  649. nonincremental search instead via `isearch-edit-string'."
  650.   (interactive)
  651.   (if (and search-nonincremental-instead 
  652.        (= 0 (length isearch-string)))
  653.       (let ((isearch-nonincremental t))
  654.     (isearch-edit-string)))
  655.   (isearch-done))
  656.  
  657.  
  658. (defun isearch-edit-string ()
  659.   "Edit the search string in the minibuffer.
  660. The following additional command keys are active while editing.
  661. \\<minibuffer-local-isearch-map>
  662. \\[exit-minibuffer] to resume incremental searching with the edited string.
  663. \\[isearch-nonincremental-exit-minibuffer] to do one nonincremental search.
  664. \\[isearch-forward-exit-minibuffer] to resume isearching forward.
  665. \\[isearch-backward-exit-minibuffer] to resume isearching backward.
  666. \\[isearch-ring-advance-edit] to replace the search string with the next item in the search ring.
  667. \\[isearch-ring-retreat-edit] to replace the search string with the previous item in the search ring.
  668. \\[isearch-complete-edit] to complete the search string using the search ring.
  669.  
  670. If first char entered is \\[isearch-yank-word], then do word search instead."
  671.  
  672.   ;; This code is very hairy for several reasons, explained in the code.
  673.   ;; Mainly, isearch-mode must be terminated while editing and then restarted.
  674.   ;; If there were a way to catch any change of buffer from the minibuffer,
  675.   ;; this could be simplified greatly.
  676.   ;; Editing doesn't back up the search point.  Should it?
  677.   (interactive)
  678.   (condition-case err
  679.       (let (isearch-nonincremental    ; should search nonincrementally?
  680.  
  681.         ;; Locally bind all isearch global variables to protect them
  682.         ;; from recursive isearching.
  683.         ;; isearch-string -message and -forward are not bound
  684.         ;; so they may be changed.  Instead, save the values.
  685.         (isearch-new-string isearch-string)
  686.         (isearch-new-message isearch-message)
  687.         (isearch-new-forward isearch-forward)
  688.         (isearch-new-word isearch-word)
  689.  
  690.         (isearch-regexp isearch-regexp)
  691.         (isearch-op-fun isearch-op-fun)
  692.         (isearch-cmds isearch-cmds)
  693.         (isearch-success isearch-success)
  694.         (isearch-wrapped isearch-wrapped)
  695.         (isearch-barrier isearch-barrier)
  696.         (isearch-adjusted isearch-adjusted)
  697.         (isearch-yank-flag isearch-yank-flag)
  698.         (isearch-invalid-regexp isearch-invalid-regexp)
  699.         (isearch-other-end isearch-other-end)
  700.         (isearch-opoint isearch-opoint)
  701.         (isearch-slow-terminal-mode isearch-slow-terminal-mode)
  702.         (isearch-small-window isearch-small-window)
  703.         (isearch-recursive-edit isearch-recursive-edit)
  704.         ;; Save current configuration so we can restore it here.
  705.         (isearch-window-configuration (current-window-configuration))
  706.         )
  707.  
  708.     ;; Actually terminate isearching until editing is done.
  709.     ;; This is so that the user can do anything without failure, 
  710.     ;; like switch buffers and start another isearch, and return.
  711.     (condition-case err
  712.         (isearch-done t)
  713.       (exit nil))            ; was recursive editing
  714.  
  715.     (isearch-message) ;; for read-char
  716.     (unwind-protect
  717.         (let* (;; Why does following read-char echo?  
  718.            ;;(echo-keystrokes 0) ;; not needed with above message
  719.            (e (let ((cursor-in-echo-area t))
  720.             (if isearch-event-data-type
  721.                 (allocate-event) (read-char))))
  722.            ;; Binding minibuffer-history-symbol to nil is a work-around
  723.            ;; for some incompatibility with gmhist.
  724.            (minibuffer-history-symbol))
  725.           ;; If the first character the user types when we prompt them
  726.           ;; for a string is the yank-word character, then go into
  727.           ;; word-search mode.  Otherwise unread that character and
  728.           ;; read a key the normal way.
  729.           ;; Word search does not apply (yet) to regexp searches,
  730.           ;; no check is made here.
  731.           (message (isearch-message-prefix nil nil t))
  732.           (if (eq 'isearch-yank-word
  733.               (lookup-key
  734.                isearch-mode-map
  735.                (char-to-string
  736.             (if isearch-event-data-type
  737.                 (or (event-to-character (next-command-event e)) 0)
  738.               e))))
  739.           (setq isearch-word t  ;; so message-prefix is right
  740.             isearch-new-word t)
  741.         (isearch-unread e))
  742.           (setq cursor-in-echo-area nil)
  743.           (setq isearch-new-string
  744.             (let (junk-ring)
  745.               (read-from-minibuffer (isearch-message-prefix)
  746.                         isearch-string
  747.                         minibuffer-local-isearch-map nil
  748.                         'junk-ring))
  749.             isearch-new-message (mapconcat 'text-char-description
  750.                            isearch-new-string "")))
  751.       ;; Always resume isearching by restarting it.
  752.       (isearch-mode isearch-forward 
  753.             isearch-regexp 
  754.             isearch-op-fun 
  755.             isearch-recursive-edit
  756.             isearch-word)
  757.  
  758.       ;; Copy new local values to isearch globals
  759.       (setq isearch-string isearch-new-string
  760.         isearch-message isearch-new-message
  761.         isearch-forward isearch-new-forward
  762.         isearch-word isearch-new-word))
  763.  
  764.     ;; Empty isearch-string means use default.
  765.     (if (= 0 (length isearch-string))
  766.         (setq isearch-string (car (if isearch-regexp regexp-search-ring
  767.                     search-ring)))
  768.       ;; This used to set the last search string,
  769.       ;; but I think it is not right to do that here.
  770.       ;; Only the string actually used should be saved.
  771.       )
  772.  
  773.     ;; Reinvoke the pending search.
  774.     (isearch-push-state)
  775.     (isearch-search)
  776.     (isearch-update)
  777.     (if isearch-nonincremental 
  778.         (progn
  779.           ;; (sit-for 1) ;; needed if isearch-done does: (message "")
  780.           (isearch-done))))
  781.  
  782.     (quit  ; handle abort-recursive-edit
  783.      (isearch-abort)  ;; outside of let to restore outside global values
  784.      )))
  785.  
  786. (defun isearch-nonincremental-exit-minibuffer ()
  787.   (interactive)
  788.   (setq isearch-nonincremental t)
  789.   (exit-minibuffer))
  790.  
  791. (defun isearch-forward-exit-minibuffer ()
  792.   (interactive)
  793.   (setq isearch-new-forward t)
  794.   (exit-minibuffer))
  795.  
  796. (defun isearch-reverse-exit-minibuffer ()
  797.   (interactive)
  798.   (setq isearch-new-forward nil)
  799.   (exit-minibuffer))
  800.  
  801.  
  802. (defun isearch-abort ()
  803.   "Abort incremental search mode if searching is successful, signalling quit.
  804. Otherwise, revert to previous successful search and continue searching.
  805. Use `isearch-exit' to quit without signalling."
  806.   (interactive)
  807. ;;  (ding)  signal instead below, if quitting
  808.   (discard-input)
  809.   (if isearch-success
  810.       ;; If search is successful, move back to starting point
  811.       ;; and really do quit.
  812.       (progn (goto-char isearch-opoint)
  813.          (isearch-done t)   ; exit isearch
  814.          (signal 'quit nil))  ; and pass on quit signal
  815.     ;; If search is failing, rub out until it is once more successful.
  816.     (while (not isearch-success) (isearch-pop-state))
  817.     (isearch-update)))
  818.  
  819.  
  820. (defun isearch-repeat (direction)
  821.   ;; Utility for isearch-repeat-forward and -backward.
  822.   (if (eq isearch-forward (eq direction 'forward))
  823.       ;; C-s in forward or C-r in reverse.
  824.       (if (equal isearch-string "")
  825.       ;; If search string is empty, use last one.
  826.       (setq isearch-string
  827.         (or (if isearch-regexp
  828.             (car regexp-search-ring)
  829.               (car search-ring))
  830.             "")
  831.         isearch-message
  832.         (mapconcat 'isearch-text-char-description
  833.                isearch-string ""))
  834.     ;; If already have what to search for, repeat it.
  835.     (or isearch-success
  836.         (progn 
  837.  
  838.           (goto-char (if isearch-forward (point-min) (point-max)))
  839.           (setq isearch-wrapped t))))
  840.     ;; C-s in reverse or C-r in forward, change direction.
  841.     (setq isearch-forward (not isearch-forward)))
  842.  
  843.   (setq isearch-barrier (point)) ; For subsequent \| if regexp.
  844.   (setq isearch-success t)
  845.   (or (equal isearch-string "")
  846.       (progn
  847.     ;; If repeating a search that found
  848.     ;; an empty string, ensure we advance.
  849.     (if (equal (match-end 0) (match-beginning 0))
  850.         (forward-char (if isearch-forward 1 -1)))
  851.     (isearch-search)))
  852.   (isearch-push-state)
  853.   (isearch-update))
  854.  
  855. (defun isearch-repeat-forward ()
  856.   "Repeat incremental search forwards."
  857.   (interactive)
  858.   (isearch-repeat 'forward))
  859.  
  860. (defun isearch-repeat-backward ()
  861.   "Repeat incremental search backwards."
  862.   (interactive)
  863.   (isearch-repeat 'backward))
  864.  
  865. (defun isearch-toggle-regexp ()
  866.   "Toggle regexp searching on or off."
  867.   ;; The status stack is left unchanged.
  868.   (interactive)
  869.   (setq isearch-regexp (not isearch-regexp))
  870.   (if isearch-regexp (setq isearch-word nil))
  871.   (isearch-update))
  872.  
  873. (defun isearch-delete-char ()
  874.   "Discard last input item and move point back.  
  875. If no previous match was done, just beep."
  876.   (interactive)
  877.   (if (null (cdr isearch-cmds))
  878.       (ding)
  879.     (isearch-pop-state))
  880.   (isearch-update))
  881.  
  882.  
  883. (defun isearch-yank (chunk)
  884.   ;; Helper for isearch-yank-word and isearch-yank-line
  885.   (let ((string (save-excursion
  886.           (and (not isearch-forward) isearch-other-end
  887.                (goto-char isearch-other-end))
  888.           (buffer-substring
  889.            (point)
  890.            (save-excursion
  891.              (cond
  892.               ((eq chunk 'word)
  893.                (forward-word 1))
  894.               ((eq chunk 'line)
  895.                (end-of-line)))
  896.              (point))))))
  897.     ;; Downcase the string if not supposed to case-fold yanked strings.
  898.     (if (and isearch-case-fold-search
  899.          (eq 'not-yanks search-upper-case))
  900.     (setq string (downcase string)))
  901.     (if isearch-regexp (setq string (regexp-quote string)))
  902.     (setq isearch-string (concat isearch-string string)
  903.       isearch-message
  904.       (concat isearch-message
  905.           (mapconcat 'isearch-text-char-description
  906.                  string ""))
  907.       ;; Don't move cursor in reverse search.
  908.       isearch-yank-flag t))
  909.   (isearch-search-and-update))
  910.  
  911.  
  912. (defun isearch-yank-word ()
  913.   "Pull next word from buffer into search string."
  914.   (interactive)
  915.   (isearch-yank 'word))
  916.  
  917. (defun isearch-yank-line ()
  918.   "Pull rest of line from buffer into search string."
  919.   (interactive)
  920.   (isearch-yank 'line))
  921.  
  922.  
  923. (defun isearch-search-and-update ()
  924.   ;; Do the search and update the display.
  925.   (if (and (not isearch-success)
  926.        ;; unsuccessful regexp search may become
  927.        ;;  successful by addition of characters which
  928.        ;;  make isearch-string valid
  929.        (not isearch-regexp))
  930.       nil
  931.     ;; In reverse search, adding stuff at
  932.     ;; the end may cause zero or many more chars to be
  933.     ;; matched, in the string following point.
  934.     ;; Allow all those possibilities without moving point as
  935.     ;; long as the match does not extend past search origin.
  936.     (if (and (not isearch-forward) (not isearch-adjusted)
  937.          (condition-case ()
  938.          (looking-at (if isearch-regexp isearch-string
  939.                    (regexp-quote isearch-string)))
  940.            (error nil))
  941.            (or isearch-yank-flag
  942.            (<= (match-end 0) 
  943.                (min isearch-opoint isearch-barrier))))
  944.     (setq isearch-success t 
  945.           isearch-invalid-regexp nil
  946.           isearch-other-end (match-end 0))
  947.       ;; Not regexp, not reverse, or no match at point.
  948.       (if (and isearch-other-end (not isearch-adjusted))
  949.       (goto-char (if isearch-forward isearch-other-end
  950.                (min isearch-opoint 
  951.                 isearch-barrier 
  952.                 (1+ isearch-other-end)))))
  953.       (isearch-search)
  954.       ))
  955.   (isearch-push-state)
  956.   (if isearch-op-fun (funcall isearch-op-fun))
  957.   (isearch-update))
  958.  
  959.  
  960. ;; *, ?, and | chars can make a regexp more liberal.
  961. ;; They can make a regexp match sooner or make it succeed instead of failing.
  962. ;; So go back to place last successful search started
  963. ;; or to the last ^S/^R (barrier), whichever is nearer.
  964. ;; + needs no special handling because the string must match at least once.
  965.  
  966. (defun isearch-*-char ()
  967.   "Handle * and ? specially in regexps."
  968.   (interactive)
  969.   (if isearch-regexp 
  970.  
  971.       (progn
  972.     (setq isearch-adjusted t)
  973.     (let ((cs (nth (if isearch-forward
  974.                5        ; isearch-other-end
  975.              2)        ; saved (point)
  976.                (car (cdr isearch-cmds)))))
  977.       ;; (car isearch-cmds) is after last search;
  978.       ;; (car (cdr isearch-cmds)) is from before it.
  979.       (setq cs (or cs isearch-barrier))
  980.       (goto-char
  981.        (if isearch-forward
  982.            (max cs isearch-barrier)
  983.          (min cs isearch-barrier))))))
  984.   (isearch-process-search-char (isearch-last-command-char)))
  985.   
  986.  
  987. (defun isearch-|-char ()
  988.   "If in regexp search, jump to the barrier."
  989.   (interactive)
  990.   (if isearch-regexp
  991.       (progn
  992.     (setq isearch-adjusted t)
  993.     (goto-char isearch-barrier)))
  994.   (isearch-process-search-char (isearch-last-command-char)))
  995.  
  996.  
  997. (defalias 'isearch-other-control-char 'isearch-other-meta-char)
  998.  
  999. (defun isearch-other-meta-char ()
  1000.   "Exit the search normally and reread this key sequence.
  1001. But only if `search-exit-option' is non-nil, the default.
  1002. If it is the symbol `edit', the search string is edited in the minibuffer
  1003. and the meta character is unread so that it applies to editing the string."
  1004.   (interactive)
  1005.   (cond ((eq search-exit-option 'edit)
  1006.      (let ((key (this-command-keys)))
  1007.        (apply 'isearch-unread (listify-key-sequence key)))
  1008.      (isearch-edit-string))
  1009.     (search-exit-option
  1010.      (let ((key (this-command-keys))
  1011.            (index 0)
  1012.            window)
  1013.        (apply 'isearch-unread (listify-key-sequence key))
  1014.        ;; Properly handle scroll-bar and mode-line clicks
  1015.        ;; for which a dummy prefix event was generated as (aref key 0).
  1016.        (and (> (length key) 1)
  1017.         (symbolp (aref key 0))
  1018.         (listp (aref key 1))
  1019.         (consp (posn-point (event-start (aref key 1))))
  1020.         (setq index 1))
  1021.        ;; If we got a mouse click, maybe it was read with the buffer
  1022.        ;; it was clicked on.  If so, that buffer, not the current one,
  1023.        ;; is in isearch mode.  So end the search in that buffer.
  1024.        (if (and (listp (aref key index))
  1025.             (setq window (posn-window (event-start (aref key index))))
  1026.             (windowp window))
  1027.            (save-excursion
  1028.          (set-buffer (window-buffer window))
  1029.          (isearch-done))
  1030.          (isearch-done))))
  1031.     (t;; otherwise nil
  1032.      (isearch-process-search-string (this-command-keys)
  1033.                     (this-command-keys)))))
  1034.  
  1035. (defun isearch-quote-char ()
  1036.   "Quote special characters for incremental search."
  1037.   (interactive)
  1038.   (isearch-process-search-char (read-quoted-char (isearch-message t))))
  1039.  
  1040. (defun isearch-return-char ()
  1041.   "Convert return into newline for incremental search.
  1042. Obsolete."
  1043.   (interactive)
  1044.   (isearch-process-search-char ?\n))
  1045.  
  1046. (defun isearch-printing-char ()
  1047.   "Add this ordinary printing character to the search string and search."
  1048.   (interactive)
  1049.   (isearch-process-search-char (isearch-last-command-char)))
  1050.  
  1051. (defun isearch-whitespace-chars ()
  1052.   "Match all whitespace chars, if in regexp mode.
  1053. If you want to search for just a space, type C-q SPC."
  1054.   (interactive)
  1055.   (if isearch-regexp 
  1056.       (if search-whitespace-regexp
  1057.       (isearch-process-search-string search-whitespace-regexp " ")
  1058.     (isearch-printing-char))
  1059.     (progn
  1060.       ;; This way of doing word search doesn't correctly extend current search.
  1061.       ;;      (setq isearch-word t)
  1062.       ;;      (setq isearch-adjusted t)
  1063.       ;;      (goto-char isearch-barrier)
  1064.       (isearch-printing-char))))
  1065.  
  1066. (defun isearch-process-search-char (char)
  1067.   ;; Append the char to the search string, update the message and re-search.
  1068.   (isearch-process-search-string 
  1069.    (isearch-char-to-string char) 
  1070.    (isearch-text-char-description char)))
  1071.  
  1072. (defun isearch-process-search-string (string message)
  1073.   (setq isearch-string (concat isearch-string string)
  1074.     isearch-message (concat isearch-message message))
  1075.   (isearch-search-and-update))
  1076.  
  1077.  
  1078. ;;===========================================================
  1079. ;; Search Ring
  1080.  
  1081. (defun isearch-ring-adjust1 (advance)
  1082.   ;; Helper for isearch-ring-adjust
  1083.   (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
  1084.      (length (length ring))
  1085.      (yank-pointer-name (if isearch-regexp
  1086.                 'regexp-search-ring-yank-pointer
  1087.                   'search-ring-yank-pointer))
  1088.      (yank-pointer (eval yank-pointer-name)))
  1089.     (if (zerop length)
  1090.     ()
  1091.       (set yank-pointer-name
  1092.        (setq yank-pointer
  1093.          (% (+ (or yank-pointer 0)
  1094.                (if advance (1- length) 1))
  1095.             length)))
  1096.       (setq isearch-string (nth yank-pointer ring)
  1097.         isearch-message (mapconcat 'isearch-text-char-description
  1098.                        isearch-string "")))))
  1099.  
  1100. (defun isearch-ring-adjust (advance)
  1101.   ;; Helper for isearch-ring-advance and isearch-ring-retreat
  1102.   (if (cdr isearch-cmds)  ;; is there more than one thing on stack?
  1103.       (isearch-pop-state))
  1104.   (isearch-ring-adjust1 advance)
  1105.   (isearch-push-state)
  1106.   (if search-ring-update
  1107.       (progn
  1108.     (isearch-search)
  1109.     (isearch-update))
  1110.     (isearch-edit-string)
  1111.     ))
  1112.  
  1113. (defun isearch-ring-advance ()
  1114.   "Advance to the next search string in the ring."
  1115.   ;; This could be more general to handle a prefix arg, but who would use it.
  1116.   (interactive)
  1117.   (isearch-ring-adjust 'advance))
  1118.  
  1119. (defun isearch-ring-retreat ()
  1120.   "Retreat to the previous search string in the ring."
  1121.   (interactive)
  1122.   (isearch-ring-adjust nil))
  1123.  
  1124. (defun isearch-ring-advance-edit (n)
  1125.   "Insert the next element of the search history into the minibuffer."
  1126.   (interactive "p")
  1127.   (let* ((yank-pointer-name (if isearch-regexp
  1128.                 'regexp-search-ring-yank-pointer
  1129.                   'search-ring-yank-pointer))
  1130.      (yank-pointer (eval yank-pointer-name))
  1131.      (ring (if isearch-regexp regexp-search-ring search-ring))
  1132.      (length (length ring)))
  1133.     (if (zerop length)
  1134.     ()
  1135.       (set yank-pointer-name
  1136.        (setq yank-pointer
  1137.          (% (+ (or yank-pointer 0)
  1138.                ;; Add LENGTH here to ensure a positive result.
  1139.                length
  1140.                (% (- n) length))
  1141.             length)))
  1142.  
  1143.       (erase-buffer)
  1144.       (insert (nth yank-pointer ring))
  1145.       (goto-char (point-max)))))
  1146.  
  1147. (defun isearch-ring-retreat-edit (n)
  1148.   "Inserts the previous element of the search history into the minibuffer."
  1149.   (interactive "p")
  1150.   (isearch-ring-advance-edit (- n)))
  1151.  
  1152. ;;(defun isearch-ring-adjust-edit (advance)
  1153. ;;  "Use the next or previous search string in the ring while in minibuffer."
  1154. ;;  (isearch-ring-adjust1 advance)
  1155. ;;  (erase-buffer)
  1156. ;;  (insert isearch-string))
  1157.  
  1158. ;;(defun isearch-ring-advance-edit ()
  1159. ;;  (interactive)
  1160. ;;  (isearch-ring-adjust-edit 'advance))
  1161.  
  1162. ;;(defun isearch-ring-retreat-edit ()
  1163. ;;  "Retreat to the previous search string in the ring while in the minibuffer."
  1164. ;;  (interactive)
  1165. ;;  (isearch-ring-adjust-edit nil))
  1166.  
  1167.  
  1168. (defun isearch-complete1 ()
  1169.   ;; Helper for isearch-complete and isearch-complete-edit
  1170.   ;; Return t if completion OK, nil if no completion exists.
  1171.   (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
  1172.          (alist (mapcar (function (lambda (string) (list string))) ring))
  1173.          (completion-ignore-case case-fold-search)
  1174.          (completion (try-completion isearch-string alist)))
  1175.     (cond
  1176.      ((eq completion t)
  1177.       ;; isearch-string stays the same
  1178.       t)
  1179.      ((or completion ; not nil, must be a string
  1180.       (= 0 (length isearch-string))) ; shouldnt have to say this
  1181.       (if (equal completion isearch-string)  ;; no extension?
  1182.       (if completion-auto-help
  1183.           (with-output-to-temp-buffer "*Isearch completions*"
  1184.         (display-completion-list 
  1185.          (all-completions isearch-string alist))))
  1186.     (setq isearch-string completion))
  1187.       t)
  1188.      (t
  1189.       (message "No completion") ; waits a second if in minibuffer
  1190.       nil))))
  1191.  
  1192. (defun isearch-complete ()
  1193.   "Complete the search string from the strings on the search ring.
  1194. The completed string is then editable in the minibuffer.
  1195. If there is no completion possible, say so and continue searching."
  1196.   (interactive)
  1197.   (if (isearch-complete1)
  1198.       (isearch-edit-string)
  1199.     ;; else
  1200.     (sit-for 1)
  1201.     (isearch-update)))
  1202.  
  1203. (defun isearch-complete-edit ()
  1204.   "Same as `isearch-complete' except in the minibuffer."
  1205.   (interactive)
  1206.   (setq isearch-string (buffer-string))
  1207.   (if (isearch-complete1)
  1208.       (progn
  1209.     (erase-buffer)
  1210.     (insert isearch-string))))
  1211.  
  1212.  
  1213. ;;;==============================================================
  1214. ;; The search status stack (and isearch window-local variables, not used).
  1215. ;; Need a structure for this.
  1216.  
  1217. (defun isearch-top-state ()
  1218.   (let ((cmd (car isearch-cmds)))
  1219.     (setq isearch-string (car cmd)
  1220.       isearch-message (car (cdr cmd))
  1221.       isearch-success (nth 3 cmd)
  1222.       isearch-forward (nth 4 cmd)
  1223.       isearch-other-end (nth 5 cmd)
  1224.       isearch-word (nth 6 cmd)
  1225.       isearch-invalid-regexp (nth 7 cmd)
  1226.       isearch-wrapped (nth 8 cmd)
  1227.       isearch-barrier (nth 9 cmd))
  1228.     (goto-char (car (cdr (cdr cmd))))))
  1229.  
  1230. (defun isearch-pop-state ()
  1231.   (setq isearch-cmds (cdr isearch-cmds))
  1232.   (isearch-top-state)
  1233.   )
  1234.  
  1235. (defun isearch-push-state ()
  1236.   (setq isearch-cmds 
  1237.     (cons (list isearch-string isearch-message (point)
  1238.             isearch-success isearch-forward isearch-other-end 
  1239.             isearch-word
  1240.             isearch-invalid-regexp isearch-wrapped isearch-barrier)
  1241.           isearch-cmds)))
  1242.  
  1243.  
  1244. ;;;==================================================================
  1245. ;; Message string
  1246.  
  1247. (defun isearch-message (&optional c-q-hack ellipsis)
  1248.   ;; Generate and print the message string.
  1249.   (let ((cursor-in-echo-area ellipsis)
  1250.     (m (concat
  1251.         (isearch-message-prefix c-q-hack ellipsis isearch-nonincremental)
  1252.         isearch-message
  1253.         (isearch-message-suffix c-q-hack ellipsis)
  1254.         )))
  1255.     (if c-q-hack m (message "%s" m))))
  1256.  
  1257. (defun isearch-message-prefix (&optional c-q-hack ellipsis nonincremental)
  1258.   ;; If about to search, and previous search regexp was invalid,
  1259.   ;; check that it still is.  If it is valid now,
  1260.   ;; let the message we display while searching say that it is valid.
  1261.   (and isearch-invalid-regexp ellipsis
  1262.        (condition-case ()
  1263.        (progn (re-search-forward isearch-string (point) t)
  1264.           (setq isearch-invalid-regexp nil))
  1265.      (error nil)))
  1266.   ;; If currently failing, display no ellipsis.
  1267.   (or isearch-success (setq ellipsis nil))
  1268.   (let ((m (concat (if isearch-success "" "failing ")
  1269.            (if isearch-wrapped "wrapped ")
  1270.            (if isearch-word "word " "")
  1271.            (if isearch-regexp "regexp " "")
  1272.            (if nonincremental "search" "I-search")
  1273.            (if isearch-forward ": " " backward: ")
  1274.            )))
  1275.     (aset m 0 (upcase (aref m 0)))
  1276.     m))
  1277.  
  1278.  
  1279. (defun isearch-message-suffix (&optional c-q-hack ellipsis)
  1280.   (concat (if c-q-hack "^Q" "")
  1281.       (if isearch-invalid-regexp
  1282.           (concat " [" isearch-invalid-regexp "]")
  1283.         "")))
  1284.  
  1285.  
  1286. ;;;========================================================
  1287. ;;; Searching
  1288.  
  1289. (defun isearch-search ()
  1290.   ;; Do the search with the current search string.
  1291.   (isearch-message nil t)
  1292.   (if (and isearch-case-fold-search search-upper-case)
  1293.       (setq isearch-case-fold-search
  1294.         (isearch-no-upper-case-p isearch-string isearch-regexp)))
  1295.   (condition-case lossage
  1296.       (let ((inhibit-quit nil)
  1297.         (case-fold-search isearch-case-fold-search))
  1298.     (if isearch-regexp (setq isearch-invalid-regexp nil))
  1299.     (setq isearch-success
  1300.           (funcall
  1301.            (cond (isearch-word
  1302.               (if isearch-forward
  1303.               'word-search-forward 'word-search-backward))
  1304.              (isearch-regexp
  1305.               (if isearch-forward
  1306.               're-search-forward 're-search-backward))
  1307.              (t
  1308.               (if isearch-forward 'search-forward 'search-backward)))
  1309.            isearch-string nil t))
  1310.     (if isearch-success
  1311.         (setq isearch-other-end
  1312.           (if isearch-forward (match-beginning 0) (match-end 0)))))
  1313.  
  1314.     (quit (isearch-unread ?\C-g)
  1315.       (setq isearch-success nil))
  1316.  
  1317.     (invalid-regexp 
  1318.      (setq isearch-invalid-regexp (car (cdr lossage)))
  1319.      (if (string-match
  1320.       "\\`Premature \\|\\`Unmatched \\|\\`Invalid "
  1321.       isearch-invalid-regexp)
  1322.      (setq isearch-invalid-regexp "incomplete input")))
  1323.     (error
  1324.      ;; stack overflow in regexp search.
  1325.      (setq isearch-invalid-regexp (car (cdr lossage)))))
  1326.  
  1327.   (if isearch-success
  1328.       nil
  1329.     ;; Ding if failed this time after succeeding last time.
  1330.     (and (nth 3 (car isearch-cmds))
  1331.      (ding))
  1332.     (goto-char (nth 2 (car isearch-cmds)))))
  1333.  
  1334.  
  1335.  
  1336. ;;;========================================================
  1337. ;;; Highlighting
  1338.  
  1339. (defvar isearch-overlay nil)
  1340.  
  1341. (defun isearch-highlight (beg end)
  1342.   (if (null search-highlight)
  1343.       nil
  1344.     (or isearch-overlay (setq isearch-overlay (make-overlay beg end)))
  1345.     (move-overlay isearch-overlay beg end (current-buffer))
  1346.     (overlay-put isearch-overlay 'face
  1347.          (if (internal-find-face 'isearch nil)
  1348.              'isearch 'region))))
  1349.  
  1350. (defun isearch-dehighlight (totally)
  1351.   (if isearch-overlay
  1352.       (delete-overlay isearch-overlay)))
  1353.  
  1354. ;;;===========================================================
  1355. ;;; General utilities
  1356.  
  1357.  
  1358. (defun isearch-no-upper-case-p (string regexp-flag)
  1359.   "Return t if there are no upper case chars in STRING.
  1360. If REGEXP-FLAG is non-nil, disregard letters preceeded by `\\' (but not `\\\\')
  1361. since they have special meaning in a regexp."
  1362.   (let ((case-fold-search nil))
  1363.     (not (string-match (if regexp-flag "\\(^\\|\\\\\\\\\\|[^\\]\\)[A-Z]"
  1364.              "[A-Z]")
  1365.                string))))
  1366.  
  1367.  
  1368. ;;;=================================================
  1369. ;; Portability functions to support various Emacs versions.
  1370.  
  1371. ;; To quiet the byte-compiler.
  1372. (defvar unread-command-event)
  1373. (defvar unread-command-events)
  1374. (defvar last-command-event)
  1375.  
  1376. (defun isearch-char-to-string (c)
  1377.   (if (integerp c)
  1378.       (make-string 1 c)
  1379.     (if (and (symbolp c) (get c 'ascii-character))
  1380.     (make-string 1 (get c 'ascii-character))
  1381.       (make-string 1 (event-to-character c)))))
  1382.  
  1383. (defun isearch-text-char-description (c)
  1384.   (isearch-char-to-string c))
  1385.  
  1386. (defun isearch-unread (&rest char-or-events)
  1387.   ;; General function to unread characters or events.
  1388.   (if isearch-gnu-emacs-events
  1389.       (setq unread-command-events
  1390.         (append char-or-events unread-command-events))
  1391.     (let ((char (if (cdr char-or-events)
  1392.             (progn
  1393.               (while (cdr char-or-events)
  1394.             (setq char-or-events (cdr char-or-events)))
  1395.               (+ 128 (car char-or-events)))
  1396.           (car char-or-events))))
  1397.       (if isearch-event-data-type
  1398.       (setq unread-command-event char)
  1399.     (setq unread-command-char char)))))
  1400.  
  1401. (defun isearch-last-command-char ()
  1402.   ;; General function to return the last command character.
  1403.   (if isearch-event-data-type
  1404.       last-command-event
  1405.     last-command-char))
  1406.  
  1407. ;;; isearch.el ends here
  1408.